;(function(){
	'use strict';
	angular.module('services.analysisSysOM.cleanCount')
	.factory('timeTrendAnalysisServices',timeTrendAnalysisServices);
	function timeTrendAnalysisServices(Restangular,CtrlShowAndHide,cleanCountService,CoreService,CoreLoading){
		return {
			pageBind: pageBind,
			getSendData: getSendData,
			getTitle: getTitle,
			timeTrendAnalysis: timeTrendAnalysis,
			search: search
		};
		function pageBind(){
			return {
				time:'w',
				ccBusinessCode:'发票',
				provinceCodes:[]
			};
		}
		function getSendData(sendData,ccBusinessType){
			var send = {
				weekOrMonth: sendData.time,
				ccBusinessCodes: [],
				provinceCodes:[]
			};
			ccBusinessType.map(function(bt){
				if(sendData.ccBusinessCode === bt.ccBusinessName){
					send.ccBusinessCodes.push(bt.ccBusinessCode);
				}
			});
			if(sendData.provinceCodes.length === 0){
				send.provinceCodes.length = 0;
			}else {
				send.provinceCodes.push(sendData.provinceCodes.provinceCode);
			}
			return send;
		}
		function timeTrendAnalysis(params,callback){
			Restangular.all('sas/analysisSysOM/cleanCount/queryTimeTrendAnalysis').post(params).then(callback);
		}
		function getTitle(sendData){
			var timeTitle = sendData.time === 'w' ? '最近一周' : '最近一月';
			return timeTitle + sendData.ccBusinessCode + '业务处理时长';
		}
		function search(sendData,ccBusinessType){
			var flag = CtrlShowAndHide.flag;
			flag.state = !flag.state;
			var send = getSendData(sendData,ccBusinessType);
			if(!flag.state){
				CoreLoading.setState(true);
				flag.show = 'view';
				send.title = getTitle(sendData);
				timeTrendAnalysis(send,function(result){
					if(result.state === '10000' && result.data.length >= 1){
						var myEcharts = echarts.init(document.getElementById('doc'));
						flag.items = result.data;
						flag.title = send.title;
						var option = cleanCountService.getEchartsInstance(result,flag.title,'time','handlingTime','业务处理时长','line');
						option.tooltip = {
							show:true,
							formatter: function(params){
								return params.name+'<br/>'+params.seriesName+': '+params.value+'(MS)';
							}
						};
						myEcharts.setOption(option);
						cleanCountService.exportView(send,flag,myEcharts);
						flag.arrow = true;
					}else if(result.data.length === 0){
						flag.state = true;
						flag.arrow = false;
						CoreService.dialog('查询无数据');
					}else {
						flag.state = true;
						flag.arrow = false;
						CoreService.dialog('查询错误');
					}
					CoreLoading.setState(false);
				});
			}
		}
	}
	timeTrendAnalysisServices.$inject = ['Restangular','CtrlShowAndHide','cleanCountService','CoreService','CoreLoading'];
})();